|
|
|
|
|
|
|
Private Declare Function CLSIDFromProgID Lib "ole32.dll" _
(ByVal lpszProgId As Long, lpclsid As CLSID) As Long |
|
|
|
|
|
|
|
|
You could define a temporary byte array and assign it from the string, then pass a pointer to the first byte of the array using code like this: |
|
|
|
|
|
|
|
|
Dim TempBuffer() As Byte
TempBuffer() = MyProgramId
res = CLSIDFromProgID(VarPtr(TempBuffer(0)), cid) |
|
|
|
|
|
|
|
|
But the code in the actual example takes a somewhat more efficient approach by using the StrPtr operator. This operator returns a pointer to the string data as it is actually stored within Visual Basic. Since Visual Basic stores strings in Unicode format, this provides an easy way to pass a Unicode string to a function. |
|
|
|
|
|
|
|
|
Option Explicit
Public Function GetCLSID() As CLSID
Dim cid As CLSID
Dim res As Long
Dim MyProgramId As String
MyProgramId = "GUIDPuzzle.GUIDObject" & Chr$(0)
res = CLSIDFromProgID(StrPtr(MyProgramId), cid)
If res = 0 Then
GetCLSID = cid
End If
End Function |
|
|
|
|
|
|
|
|
Why didn't we use VarPtr(MyProgramId) in the CLSIDFromProgID call? |
|
|
|
|
|
|
|
|
Because the VarPtr operator returns the address of the MyProgramId variable. This variable contains a pointer to the BSTR string data. The difference between these two operators can be seen in Figure S26-1. |
|
|
|
|
|
|
|
|
The results of this solution can be seen in Figure S26-2. |
|
|
|
|
|
|
|
|
Now let's take a look at the structure data and the raw data as shown in the figures and in Table S26-1. Note that the actual values in each case will differ on your system. |
|
|
|
|
|